package com.zhiyume.service;

import com.zhiyume.dao.api.TVersionMapper;
import com.zhiyume.dao.entity.TVersion;
import com.zhiyume.dao.entity.TVersionCriteria;
import com.zhiyume.dao.entity.TVersion;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: TVersionService.java, v 0.1 2018-08-07 19:00:11 wang Exp $$
 */

@Service
public class TVersionService {
    @Autowired
    private TVersionMapper tVersionMapper;

    /**
     * 根据Id查询信息
     * @return
     */
    public TVersion findVersionById(long id){
        TVersionCriteria example = new TVersionCriteria();
        TVersionCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TVersion> versionList = tVersionMapper.selectByExample(example);
        if(null != versionList && versionList.size() > 0) {
            return versionList.get(0);
        }

        return new TVersion();
    }


    /**
     * 添加信息
     * @param tVersion     * @return
     */
    public TVersion addVersion(TVersion tVersion) throws Exception{

        long id = GenerateKeyUtil.generationPK();
        tVersion.setId(id);

        tVersionMapper.insert(tVersion);
        
        return tVersion;
    }
    
    
    /**
     * 修改人员信息
     * @param tVersion     * @return
     */
    public int updateVersion(TVersion tVersion) {

        TVersionCriteria example = new TVersionCriteria();
        TVersionCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tVersion.getId());
        return tVersionMapper.updateByExample(tVersion,example);
    }
    
     /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int deleteVersion(long id) {
        TVersionCriteria example = new TVersionCriteria();
        TVersionCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);

        return tVersionMapper.deleteByExample(example);
    }


    public List<TVersion> queryVersionList(TVersion dto,PageReq page) {
        TVersionCriteria tVersionCriteria = new TVersionCriteria();
        TVersionCriteria.Criteria criteria = tVersionCriteria.createCriteria();
        tVersionCriteria.setLimitStart(page.getPage()*page.getPageSize());
        tVersionCriteria.setLimitEnd(page.getPageSize()*page.getPageSize()+page.getPageSize());

        createCriteria(dto, criteria);
        return tVersionMapper.selectByExample(tVersionCriteria);
    }



    public int countVersion(TVersion dto) {
        TVersionCriteria tVersionCriteria = new TVersionCriteria();
        TVersionCriteria.Criteria criteria = tVersionCriteria.createCriteria();

        createCriteria(dto, criteria);
        return tVersionMapper.countByExample(tVersionCriteria);
    }
    
    private void createCriteria(TVersion dto, TVersionCriteria.Criteria criteria) {
        if(dto.getvId()!=null&&-1!=dto.getvId()){
            criteria.andVIdEqualTo(dto.getvId());
        }
        if(dto.getId()!=null&&-1!=dto.getId()){
            criteria.andIdEqualTo(dto.getId());
        }
        if(StringUtil.isNotBlank(dto.getTitle())){
            criteria.andTitleLike("%"+dto.getTitle()+"%");
        }
        if(StringUtil.isNotBlank(dto.getContent())){
            criteria.andContentLike("%"+dto.getContent()+"%");
        }
        if(dto.getCreateId()!=null&&-1!=dto.getCreateId()){
            criteria.andCreateIdEqualTo(dto.getCreateId());
        }
        if(StringUtil.isNotBlank(dto.getCreateTime())){
            criteria.andCreateTimeLike("%"+dto.getCreateTime()+"%");
        }
        if(dto.getUpdateId()!=null&&-1!=dto.getUpdateId()){
            criteria.andUpdateIdEqualTo(dto.getUpdateId());
        }
        if(StringUtil.isNotBlank(dto.getUpdateTime())){
            criteria.andUpdateTimeLike("%"+dto.getUpdateTime()+"%");
        }
                
    }

}
